package com.dream.root.common.xss;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by liulinhui on 2017/12/15 2:06
 */
public class SQLFilter {
    /**
     * SQL注入过滤
     *
     * @param str 待验证的字符串
     */
    public static List<String> sqlInject(String str) {
        if (StringUtils.isBlank(str)) {
            return null;
        }
        //去掉'|"|;|\字符
        str = StringUtils.replace(str, "'", "");
        str = StringUtils.replace(str, "\"", "");
        str = StringUtils.replace(str, ";", "");
        str = StringUtils.replace(str, "\\", "");

        //转换成小写
        str = str.toLowerCase();

        //非法字符
        String[] keywords = {"master", "truncate", "insert", "select", "delete", "update", "declare", "alert", "drop"};

        List<String> invalidWords = new ArrayList<>();

        //判断是否包含非法字符
        for (String keyword : keywords) {
            if (str.contains(keyword)) {
                invalidWords.add(keyword);
            }
        }

        return invalidWords;
    }
}
